package com.cy.deepseeksport.NLP;

import ch.qos.logback.core.util.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class ParseWebSearchResponse {
    // 时区设置（根据实际需求调整）
    private static final ZoneId ZONE = ZoneId.of("Asia/Shanghai");
    // 日期时间格式（带中文时区标识）
    private static final DateTimeFormatter DATE_FORMATTER =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss (zzz)");

    public static void main(String[] args) throws IOException {
        File file = new File("C:\\project\\sportdeepseek\\src\\main\\resources\\researchContent.txt");
        String resp = FileUtils.readFileToString(file, "utf-8");

        StringBuilder analysisData = new StringBuilder();
        analysisData.append("=== 网络信息分析数据集 ===\n\n");

        try {
            JSONObject respJson = JSON.parseObject(resp);
            JSONArray news = respJson.getJSONArray("value");

            for (int i = 0; i < news.size(); i++) {
                try {
                    JSONObject item = news.getJSONObject(i);
                    analysisData.append(buildAnalysisEntry(item)).append("\n\n");
                } catch (Exception e) {
                    System.err.println("解析第 " + (i+1) + " 条数据失败: " + e.getMessage());
                }
            }
        } catch (Exception e) {
            System.err.println("解析响应失败: " + e.getMessage());
        }

        System.out.println(analysisData.toString());
    }

    public static String buildAnalysisEntry(JSONObject item) {
        StringBuilder entry = new StringBuilder();

        // 标题处理（带非法字符过滤）
        String title = item.getString("name");
        entry.append("▌ 标题：").append(cleanText(title, "无标题"));

        // 时间处理（带时区转换）
        String isoDate = item.getString("dateLastCrawled");
        entry.append("\n   时间：").append(formatDateTime(isoDate));

        // 完整摘要保留（仅做基础清洗）
        String summary = item.getString("summary");
        entry.append("\n   完整内容：").append(cleanText(summary, "无可用内容"));

        // 来源信息（如果存在）
        if (item.containsKey("url")) {
            entry.append("\n   原始链接：").append(item.getString("url"));
        }

        return entry.toString();
    }

    private static String formatDateTime(String isoString) {
        try {
            Instant instant = Instant.parse(isoString);
            return DATE_FORMATTER.format(instant.atZone(ZONE));
        } catch (Exception e) {
            return "时间解析失败：" + isoString;
        }
    }

    private static String cleanText(String text, String defaultValue) {
        if (text == null || text.trim().isEmpty()) return defaultValue;

        // 基础清洗：去除控制字符、合并空格
        return text.replaceAll("[\n\r\t]", " ")  // 替换控制字符
                .replaceAll(" +", " ")        // 合并多个空格
                .trim();
    }
}
